The Python subprocess
module supports running programs from within Python. It has a variety of functions for running programs in different ways, as described in the Python subprocess module documentation. The shlex
module is a useful companion for splitting up command lines to use with the subprocess module.
In [15]:
import os
print os.environ['PATH']
In [11]:
import subprocess
import shlex
import sys
import os
print "working directory:", os.getcwd()
cmd_str = 'echo "Hello World"'
cmd = shlex.split(cmd_str)
print cmd
subprocess.check_output(cmd)
Out[11]:
In [12]:
subprocess.call(['echo', 'Hello World'])
Out[12]:
In [21]:
output_file = open('/home/pvh/Documents/python/grep_output.txt', 'w')
print output_file
with output_file:
exit_code = subprocess.call(['grep','pvh','/etc/passwd'], stdout=output_file)
print "Done", exit_code
In [41]:
output_file = open('/home/pvh/Documents/python/grep_output.txt', 'w')
err_out_file = open('/home/pvh/Documents/python/grep_error.txt', 'w')
exit_code = subprocess.call(['grep','pvh','/etc/passssswd'],
stdout=output_file,
stderr=err_out_file)
output_file.close()
err_out_file.close()
print "Done", exit_code
In [ ]:
output_file = open('/home/pvh/Documents/python/grep_output.txt', 'w')
err_out_file = open('/home/pvh/Documents/python/grep_error.txt', 'w')
exit_code = subprocess.call(['grep','pvh','/etc/passssswd'],
stdout=output_file,
stderr=subprocess.STDOUT)
output_file.close()
err_out_file.close()
print "Done", exit_code
In [17]:
subprocess.call(['/opt/Zotero_linux-x86_64/zotero'])
print "Hello Everyone"
In [26]:
input_file = open('python.fasta')
output_file = open('one_orchid.txt', 'w')
cmd_str = 'grep "P.villosum"'
cmd = shlex.split(cmd_str)
subprocess.call(cmd, stdin=input_file, stdout=output_file)
Out[26]:
In [36]:
input_file = open('python.fasta')
cmd_str = 'grep "P.villosum"'
cmd = shlex.split(cmd_str)
results = subprocess.check_output(cmd, stdin=input_file)
print "I found P.villosum in {}".format(results.split()[0][1:])
In [38]:
cmd_str = 'grep "P.villosum" /etc/fishfishfish'
cmd = shlex.split(cmd_str)
try:
results = subprocess.check_output(cmd)
except subprocess.CalledProcessError:
print >>sys.stderr,"Running the command ({}) failed".format(cmd_str)
else:
print "I got a result:", results
In [44]:
name = "Peter"
cmd = "echo Hello {}".format(name)
subprocess.check_output(cmd, shell=True)
Out[44]:
In [45]:
name = "Peter ; echo And now for something evil"
cmd = "echo Hello {}".format(name)
subprocess.check_output(cmd, shell=True)
Out[45]:
In [51]:
proc1 = subprocess.Popen(['/opt/Zotero_linux-x86_64/zotero'])
proc2 = subprocess.Popen(['/usr/bin/gedit'])
proc1.wait()
proc2.wait()
Out[51]:
In [50]:
subprocess.call(['/opt/Zotero_linux-x86_64/zotero'])
subprocess.call(['/usr/bin/gedit'])
Out[50]:
In [ ]:
proc1 = subprocess.Popen(['/opt/Zotero_linux-x86_64/zotero'])
proc2 = subprocess.Popen(['/usr/bin/gedit'])
proc1.wait()
proc2.wait()
In [56]:
cmd_str = 'grep "P.villosum" python.fasta'
cmd = shlex.split(cmd_str)
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
(proc_output, proc_error) = proc.communicate()
exit_code = proc.wait()
print "output:", proc_output
print "error:", proc_error
print "exit code:", exit_code
In [57]:
cmd_str = 'grep "P.villosum" python.fasta'
cmd = shlex.split(cmd_str)
proc = subprocess.Popen(cmd)
(proc_output, proc_error) = proc.communicate()
exit_code = proc.wait()
print "output:", proc_output
print "error:", proc_error
print "exit code:", exit_code
In [59]:
cmd_str = "grep chicken"
cmd = shlex.split(cmd)
data = """chicken
fish
pork
chicken
snake
aardvark"""
#data = "chicken\nfish"
proc = subprocess.Popen(cmd, stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
(proc_output, proc_error) = proc.communicate(data)
print "output:", proc_output
print "error:", proc_error
exit_code = proc.wait()
print "exit code:", exit_code
In [60]:
cmd_str = 'sleep 60'
cmd = shlex.split(cmd_str)
count = 4
procs = []
for i in range(count):
procs.append(subprocess.Popen(cmd))
for proc in procs:
proc.wait()
In [ ]: